package com.xizi.lotterySystem.controller;

import com.xizi.lotterySystem.common.exception.ControllerException;
import com.xizi.lotterySystem.common.exception.errorCode.ControllerErrorCodeException;
import com.xizi.lotterySystem.common.pojo.CommonResult;
import com.xizi.lotterySystem.controller.param.DrawPrizeParam;
import com.xizi.lotterySystem.controller.param.ShowWinningRecordsParam;
import com.xizi.lotterySystem.controller.result.WinningRecordResult;
import com.xizi.lotterySystem.service.IDrawPrizeService;
import com.xizi.lotterySystem.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/draw")
public class DrawPrizeController {
    private static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IDrawPrizeService drawPrizeService;

    @RequestMapping("/prize")
    public CommonResult<Boolean> drawPrize(@RequestBody @Validated DrawPrizeParam param) {
        if (param == null) {
            log.error("抽奖失败！ param 为 null");
            throw new ControllerException(ControllerErrorCodeException.PARAM_ERROR);
        }
        drawPrizeService.drawPrize(param);

        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(@RequestBody
                                                                  @Validated ShowWinningRecordsParam param) {
        if (param.getActivityId() == null) {
            log.error(ControllerErrorCodeException.PARAM_ERROR.getMessage());
            throw new ControllerException(ControllerErrorCodeException.PARAM_ERROR);
        }

        List<WinningRecordDTO> wRDTOList = drawPrizeService.selectWinningRecords(param);

        return CommonResult.success(convertToWinningRecordResult(wRDTOList));
    }

    private List<WinningRecordResult> convertToWinningRecordResult(List<WinningRecordDTO> wRDTOList) {
        if (wRDTOList == null) {
            log.info("查询的中奖者名单为空！");
            return new ArrayList<WinningRecordResult>();
        }

        return wRDTOList.stream()
                .map(
                        w -> {
                            WinningRecordResult result = new WinningRecordResult();
                            result.setWinnerId(w.getWinnerId());
                            result.setWinnerName(w.getWinnerName());
                            result.setPrizeName(w.getPrizeName());
                            result.setPrizeTier(w.getPrizeTier());
                            result.setWinningTime(w.getWinningTime());
                            return result;
                        }).collect(Collectors.toList());
    }


}
